﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ServiceBase;
namespace ServicePrice
{
    public class www_LeFeng_com : IServicePrice
    {
        private Dictionary<string, char> _matrix = new Dictionary<string, char>();
        public www_LeFeng_com()
        {
            _matrix.Add("11110011110111001110001111110000111111000111111100011111110000001100000111111100011111110000001100000000110000", '￥');
            _matrix.Add("1111111100111111111011111111101100011110000001111000000111100000111110000111110000111111000111111000111111000011111111111111111111", '2');
            _matrix.Add("0111111100111111111011111111111111001111111000111111110001111111101111111111111101111111110000001111000011111001111111100111111000", '9');
            _matrix.Add("111111111", '.');
            _matrix.Add("0111111110011111111011111111111111001111111100111111100001111110000111111000011111110011111111001111111111111101111111100111111110", '0');
            _matrix.Add("011111111011111111011100000011100000011111110011111111000011111000001111000000111110001111111011111111111111111111110", '5');
            _matrix.Add("0111111110111111111111111111111111000111111111111111111111100111111110111111111111110011111111000111111111111111111111110111111110", '8');
            _matrix.Add("000111000011111000111111000111111000000111000000111000000111000000111000000111000000111000000111000111111111111111111", '1');
            _matrix.Add("0001111110011111111001111100001111000000111111111011111111111111011111111000111111110001111111001111111111111101111111110011111110", '6');
            _matrix.Add("111111110111111111111111111110000111000011111000111110000111111000011111000001111110001111111011111111111111111111110", '3');
            _matrix.Add("111111111111111111000001111000001111000011110000111110000111100001111100001111000011111000011110000111100000111100000", '7');
            _matrix.Add("0000111100000111110000011111000011111100011111110001110111001111011100111001110011111111111111111111000001110000000111000000011100", '4');
        }
        /// <summary>
        /// 识别一张图片价格，并返回图片上的值。
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string DoRecognition(string url)
        {
            //下载图片
            Bitmap map = GetBitmap.From_URL(url);
            //处理成可识别的图片
            Bitmap[] list = Dispose(map);
            //识别
            string result = Match.Match_Dictionary.ByDictionary(list, _matrix);
            return result;
        }

        private Bitmap[] Dispose(Bitmap map)
        {
            //二值化
            SimpleColor.TwoColor2(map);
            //按空列切割
            Bitmap[] list = Split.ByEmptyColumn(map);
            //切割粘连片段
            list = Split.ByLimitWidth(list, 11, 10);
            return list;
        }

        public Bitmap[] ViewMatrix(string dir)
        {
            Bitmap[] sourceList = GetBitmap.From_Folder(dir);
            List<Bitmap> all = new List<Bitmap>();
            foreach (Bitmap map in sourceList)
            {
                Bitmap[] list = Dispose(map);
                all.AddRange(list);
            }
            Bitmap[] matrix = ServiceMatrix.Pick_Matrix.ByBitmapList(all.ToArray());
            Helper_View.View.View_Bitmap(matrix);
            return matrix;
        }

        public void CreateMatrix(string dir)
        {
            Bitmap[] matrix = ViewMatrix(dir);
            ServiceMatrix.Builder_Matrix.Create(matrix, Guid.NewGuid().ToString() + ".txt", ServiceMatrix.Builder_Matrix.MatrixType.零一字符串);
        }
    }
}
